package com.whacos.modules.ordering.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whacos.common.annotations.Log;
import com.whacos.common.controller.BaseController;
import com.whacos.common.enums.Constant;
import com.whacos.common.enums.DelFlagEnum;
import com.whacos.common.utils.BeanUtils;
import com.whacos.common.utils.DateUtils;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.ordering.entity.Shop;
import com.whacos.modules.ordering.service.ShopService;
import com.whacos.modules.ordering.vo.ShopVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 商铺信息
 *
 * @author xiongdun
 * @email 1274238268@qq.com
 * @date 2018-12-10 23:38:20
 */
@Controller
@RequestMapping("/ordering/shop")
@Slf4j
public class ShopController extends BaseController {
    @Autowired
    private ShopService shopService;

    @GetMapping()
    @RequiresPermissions("ordering:shop:shop")
    public String Shop() {
        return "ordering/shop/shop";
    }

    // @Log("查询商铺信息列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("ordering:shop:shop")
    public IPage<ShopVO> list(ShopVO shopVO) {
        //查询列表数据
        IPage<ShopVO> shopVOPage = new Page<>();
        try {
            // 请求参数
            // 查询数据
            IPage<Shop> shopPage = shopService.page(new Page<>(shopVO.getPageNum(), shopVO.getPageSize()),
                    new LambdaQueryWrapper<Shop>()
                            .orderByDesc(Shop::getUpdatedDate));
            // 返回结果
            BeanUtils.copyPage(shopPage, shopVOPage);
            List<ShopVO> shopVOList = new ArrayList<>();
            shopPage.getRecords().forEach(source -> {
                ShopVO target = new ShopVO();
                BeanUtils.copyProperties(source, target);
                shopVOList.add(target);
            });
            shopVOPage.setRecords(shopVOList);
        } catch (Exception e) {
            log.error("查询商铺信息列表异常，异常原因：" + e.getMessage(), e);
        }
        return shopVOPage;
    }

    // @Log("新增商铺信息")
    @GetMapping("/add")
    @RequiresPermissions("ordering:shop:add")
    public String add() {
        return "ordering/shop/add";
    }

    // @Log("跳转到修改商铺信息")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("ordering:shop:edit")
    public String edit(@PathVariable("id") Long id, Model model) {
        Shop shop = shopService.getById(id);
        model.addAttribute("shop", shop);
        return "ordering/shop/edit";
    }

    @Log("保存商铺信息")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("ordering:shop:add")
    public ResponseVO save(ShopVO shopVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            // 转换请求参数
            Shop shop = new Shop();
            BeanUtils.copyProperties(shopVO, shop);
            // 保存数据
            shop.createdData(this.getUserId());
            if (shopService.save(shop)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("修改商铺信息")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("ordering:shop:edit")
    public ResponseVO update(ShopVO shopVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            Shop shop = new Shop();
            BeanUtils.copyProperties(shopVO, shop);
            shop.updatedData(this.getUserId());
            if (shopService.updateById(shop)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("删除商铺信息")
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("ordering:shop:remove")
    public ResponseVO remove(@RequestParam(value = "id") Long id) {
        if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            Shop shop = new Shop();
            shop.setId(id);
            shop.updatedData(this.getUserId());
            if (shopService.updateById(shop)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("批量删除商铺信息")
    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("ordering:shop:batchRemove")
    public ResponseVO batchRemove(@RequestParam("ids[]") List<Long> ids) {
        if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            List<Shop> shopList = new ArrayList<>();
            ids.forEach(id -> {
                Shop shop = new Shop();
                shop.setId(id);
                shop.updatedData(this.getUserId());
                shopList.add(shop);
            });
            if (shopService.updateBatchById(shopList)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

}
